package com.nchu.student_score.service.impl;

import com.nchu.student_score.dataObject.PasswordCheck;
import com.nchu.student_score.dataObject.User;
import com.nchu.student_score.mapper.AdminMapper;
import com.nchu.student_score.mapper.StudentMapper;
import com.nchu.student_score.mapper.TeacherMapper;
import com.nchu.student_score.model.Admin;
import com.nchu.student_score.model.Student;
import com.nchu.student_score.model.Teacher;
import com.nchu.student_score.service.LoginService;
import com.nchu.student_score.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void md5Test(User user){
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        System.out.println(md5Password);
    }

    @Override
    public ResultVo<User> adminLogin(User user) {
        Admin admin;
        ResultVo<User> userResultVo = new ResultVo<>();

        try{//将字符串类型的用户名转换成id
            admin = adminMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            userResultVo.setStatus(1);
            userResultVo.setMessage("用户名不正确!");
            return userResultVo;
        }

        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));//对输入的密码进行MD5加密

        if(admin == null || !(admin.getPassword().equals(user.getPassword()))){
            userResultVo.setStatus(2);
            userResultVo.setMessage("用户名或密码不正确!");
            return userResultVo;
        }

        user.setName(admin.getAdminName());
        user.setPassword(PasswordCheck.getUserKey(admin.getPassword()));
        userResultVo.setObj(user);
        userResultVo.setStatus(0);
        userResultVo.setMessage("管理员登录成功!");

        return userResultVo;
    }

    @Override
    public ResultVo<User> teacherLogin(User user) {
        Teacher teacher;
        ResultVo<User> userResultVo = new ResultVo<>();

        try{//将字符串类型的用户名转换成id
            teacher = teacherMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            userResultVo.setStatus(1);
            userResultVo.setMessage("用户名不正确!");
            return userResultVo;
        }

        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));//对输入的密码进行MD5加密

        if(teacher == null || !(teacher.getTeacherPassword().equals(user.getPassword()))){
            userResultVo.setStatus(2);
            userResultVo.setMessage("用户名或密码不正确!");
            return userResultVo;
        }

        user.setName(teacher.getTeacherName());
        user.setPassword(PasswordCheck.getUserKey(teacher.getTeacherPassword()));
        userResultVo.setObj(user);
        userResultVo.setStatus(0);
        userResultVo.setMessage("教师登录成功!");

        return userResultVo;
    }

    @Override
    public ResultVo<User> studentLogin(User user) {
        Student student;
        ResultVo<User> userResultVo = new ResultVo<>();

        try{//将字符串类型的用户名转换成id
            student = studentMapper.selectByPrimaryKey(Integer.valueOf(user.getId()));
        }
        catch (NumberFormatException e){
            userResultVo.setStatus(1);
            userResultVo.setMessage("用户名不正确!");
            return userResultVo;
        }

        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));//对输入的密码进行MD5加密

        if(student == null || !(student.getStuPassword().equals(user.getPassword()))){
            userResultVo.setStatus(2);
            userResultVo.setMessage("用户名或密码不正确!");
            return userResultVo;
        }

        user.setName(student.getStuName());
        user.setPassword(PasswordCheck.getUserKey(student.getStuPassword()));
        userResultVo.setObj(user);
        userResultVo.setStatus(0);
        userResultVo.setMessage("学生登录成功!");

        return userResultVo;
    }
}
